$(function () {

    let agent = getQueryString("agent");

    let url = '../user/listAudlting?1=1';
    if (agent) {
        url += '&agent=' + agent;
    }
    
    $("#jqGrid").jqGrid({
        url: url,
        datatype: "json",
        colModel: [
            {label: 'id', align: "center", name: 'id', index: 'id', key: true, hidden: true},
            {
                label: '昵称',
                align: "center",
                name: 'nickname',
                index: 'nickname',
                width: 80,
                formatter: cLink,
                editable: true
            },
            {label: '手机', align: "center", name: 'mobile', index: 'mobile', width: 80},
            {
                label: '注册时间', align: "center", name: 'registerTime', index: 'register_time', width: 100,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },
            {
                label: '近一次登录时间', align: "center", name: 'lastLoginTime', index: 'last_login_time', width: 100,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            },
            {label: '城市', align: "center", name: 'cityName', index: 'city_name', width: 80},
            {
                label: '审核状态', align: "center", name: 'applicationStatus', index: 'applicationStatus', width: 80
                , formatter: applicationStatusLink,
                editable: true
            }

        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        postData: {
            'applicationStatus': 2
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

function applicationStatusLink(cellvalue, options, rowObject) {
    var status_var = "";
    if (rowObject.applicationStatus == 1) {
        status_var = '<span class="label label-default">未提交</span>';
        return status_var;
    }
    if (rowObject.applicationStatus == 2) {
        status_var = '<span class="label label-danger">待审核 </span>';
        return '<a href="javascript:void(0)" onclick="audinginfo(' + rowObject.id + ')">' + status_var + '</a>';
    }
    if (rowObject.applicationStatus == 3) {
        status_var = '<span class="label label-success">审核通过</span>';
        return status_var;
    }
    if (rowObject.applicationStatus == 4) {
        status_var = '<span class="label label-danger">审核失败</span>';
        return status_var;
    }
}


function cLink(cellvalue, options, rowObject) {
    return '<a href="javascript:void(0)" onclick="getinfo(' + rowObject.id + ')">' + rowObject.nickname + '</a>';
}

function getinfo(id) {
    vm.showList = false;
    vm.title = "修改";
    vm.uploadList = [];
    vm.getInfo(id);
}


function audinginfo(id) {
    openWindow({
        title: "快速审核",
        area: ['300px', '150px'],
        content: jQuery("#staticAuding"),
        btn: ['确定', '取消'],
        btn1: function (index) {
            vm.apiUser.id = id;
            layer.close(index);
            $.ajax({
                type: "POST",
                url: "../user/audlt",
                contentType: "application/json",
                data: JSON.stringify(vm.apiUser),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);

                    }

                }
            });


        }
    });
}


let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        apiUser: {},
        visible: false,
        phone: "",
        uploadList: {},
        avatar: '',
        ruleValidate: {},
        q: {
            applicationStatus: 0
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        audingInfo: function () {
            openWindow({
                title: "选择部门",
                area: ['300px', '450px'],
                content: jQuery("#staticAuding"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    layer.close(index);
                }
            });
        },
        update: function (event) {
            let id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.phone = "";
            vm.title = "修改";
            vm.uploadList = [];

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {

            var applicationStatus = vm.apiUser.applicationStatus;
            if (applicationStatus == 2 || applicationStatus == 1) {
                alert('审核状态必填');
                return;
            }
            vm.apiUser.pics = vm.uploadList;


            let url = vm.apiUser.id == null ? "../user/save" : "../user/audltUpdate";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.apiUser),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },

        getInfo: function (id) {
            $.get("../user/audlting/" + id, function (r) {
                vm.apiUser = r.apiUser;
                vm.uploadList = r.picList;
                vm.phone = r.phone;
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'name': vm.q.name
                },
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleView(name) {
            this.imgName = name;
            this.visible = true;
        }

    }
});